package edu.cuit.campus.admin.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/11 15:49
 */
@WebFilter("/*")
@Slf4j
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        var request = (HttpServletRequest) servletRequest;
        var response = (HttpServletResponse) servletResponse;

        String uri = request.getRequestURI();
        log.info("拦截的请求uri: {}", uri);
        if(uri.contains("administrators/login")){
            chain.doFilter(request, response);
            return;
        }

        //判断管理员是否登录
        var administrator = request.getSession().getAttribute("administrator");
        if(administrator == null)//未登录
            response.setStatus(401); //unauthorization
         else
            chain.doFilter(request, response);
    }
}
